package view.itemRenderes
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.flashIso.engine.base.IsoMap;
	import org.flashIso.engine.events.IsoEvent;
	import org.flashIso.engine.library.objects.LImage;
	import org.flashIso.engine.objects.IsoImage;
	
	import spark.events.TextOperationEvent;
	
	import view.itemRenderes.ItemRendererLibraryItemBase;
	
	public class ItemRendererLImage extends ItemRendererLibraryItemBase
	{
		private var object:IsoImage;
		
		public function ItemRendererLImage()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler, false, 0, true);
			textInput.addEventListener(FocusEvent.FOCUS_OUT, hideTextInput, false, 0, true);
			textInput.addEventListener(MouseEvent.CLICK, textInputClick, false, 0, true);
			addEventListener(MouseEvent.DOUBLE_CLICK, showTextInput, false, 0, true);
			object = new IsoImage();
		}
		private function keyDownHandleer(e:KeyboardEvent) : void {
			if (e.keyCode == Keyboard.ENTER){
				hideTextInput();
			}
		}
		override public function set data(value:Object):void {
			super.data = value;
			if (data != null){
				object.libraryReference = data as LImage;
			}
			
		}
		
		protected function creationCompleteHandler(event:FlexEvent):void
		{
			container.addChild(object);
			object.libraryReference = data as LImage;
			object.addEventListener(Event.CHANGE, objectDrawn, false, 0, true);
			addEventListener(MouseEvent.DOUBLE_CLICK, showTextInput, false, 0, true);
			(data as LImage).addEventListener(Event.CHANGE, objectChange, false, 0, true);
			objectDrawn();
			objectChange();
		}
		
		private function objectChange(e:Event = null): void {
			llabel.text = textInput.text = data.id;
		}
		private function objectDrawn(e:Event = null) : void {
			object.x = 0;
			object.y = 0;
			
			object.scaleX = object.scaleY = 1;
			object.scaleX = object.scaleY = Math.min(this.width/object.width, this.height/object.height, 1);
			
			var rect:Rectangle = object.getBounds(container);
			object.x = -(object.width / 2) - rect.x;
			object.y = -(object.height / 2) - rect.y;
		}
		
		
		protected function textInputClick(event:MouseEvent):void
		{
			event.stopPropagation();
		}
		
		private function showTextInput(e:Event = null) : void {
			textInput.visible = true;
			textInput.text = llabel.text;
			textInput.getFocus();
			stage.addEventListener(MouseEvent.CLICK, hideTextInput, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandleer, false, 0, true);
		}
		
		protected function hideTextInput(event:Event = null):void
		{
			if (textInput.visible == true){
				stage.removeEventListener(MouseEvent.CLICK, hideTextInput);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandleer);
				textInput.visible = false;
				if (llabel.text != textInput.text){
					dispatchEvent(new Event(Event.CHANGE));
				}
			}			
		}
	}
}